package com.share.fastJson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
public class Address {


    @JSONField(ordinal = 3)
    private String name;
    //    private transient String name;
    @JSONField(ordinal = 2, serialize = false, deserialize = false)
    private Integer id;
    @JSONField(name="birthday", format= "yyyy-MM-dd", ordinal = 1)
    private Date date;

    /*
    JSON类之toJSONString()方法，实现json对象转化为json字符串和javabean对象转化为json 字符串


    {"addresses":[{"id":1,"name":"北京"},{"id":2,"name":"天津"}],"age":26,"name":"sunxj"}
    {"id":1,"name":"北京"}
    [{"id":1,"name":"北京"},{"id":2,"name":"天津"}]

     */
    public static void main(String[] args) {
        Address address = new Address();
        address.setId(1);
        address.setName("北京");
        address.setDate(new Date());

        String s = JSON.toJSONString(address);
        System.out.println("s:toJSONString:" + s);

        System.out.println(JSON.parseObject(s,Address.class));

        Object o = JSON.toJSON(address);
        System.out.println(o);

        Address address1 = JSON.toJavaObject((JSON) o, Address.class);
        System.out.println(address1);



//        PropertyFilter filter = new PropertyFilter() {
//
//            public boolean apply(Object source, String name, Object value) {
//                if ("id".equals(name)) {
//                    int id = ((Integer) value).intValue();
//                    return id >= 100;
//                }
//                return false;
//            }
//        };
//
//        JSON.toJSONString(address, filter);

//        ArrayList<Address> list = new ArrayList<>();
//        Address address1 = new Address();
//        address1.setId(2);
//        address1.setName("天津");
//        list.add(address);
//        list.add(address1);
//        String s1 = JSON.toJSONString(list);
//        System.out.println("s1:toJSONString:" + s1);


//        JSONObject jsonObject = JSON.parseObject(s);
//        System.out.println("s:parseObject:" + jsonObject);
//
//
//        Address address2 = JSON.parseObject(s, Address.class);
//        System.out.println("s:parseObject:" + address2);
//
//        JSONArray objects = JSON.parseArray(s1);
//        System.out.println("objects : " + objects);
//
//        List<Address> list1 = JSON.parseArray(s1, Address.class);
//        System.out.println("list1 : " + list1);



    }


}
